﻿package com.thomasbrault.core 
{
	import com.greensock.TweenLite;

	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;

	/**
	 * @author thomas
	 */
	public class SoundManager extends EventDispatcher
	{

		private static var _instance : SoundManager;
		private static var _soundChannelList : Array;
		
		public static var IS_MUTE : Boolean = false;
		
		public var temp_volume : Number = 0;
		
		public static function get instance ( ) : SoundManager
		{
			
			if ( _instance == null ) 
			{
				_instance 			= new SoundManager();
				_soundChannelList 	= new Array();
			}
			
			return _instance;
		}
		
		public static function toggleMute () : void
		{
			if ( _instance == null ) {
				_instance 			= new SoundManager();
				_soundChannelList 	= new Array();
			}
			
			var channel : SoundChannel;			
			if ( IS_MUTE )
			{
				for ( var i in _soundChannelList )
					channel = _soundChannelList[i];

				_instance.temp_volume = 0;
				TweenLite.to(_instance, 0.5, {temp_volume : 1, onUpdate: _instance.updateGlobalVolume} );
				
				SoundMixer.soundTransform = new SoundTransform(1,0);
			} 
			else {
				for ( var j in _soundChannelList )
					channel = _soundChannelList[j];
					
				_instance.temp_volume = 1;
				
				TweenLite.to(_instance, 0.5, {temp_volume : 0, onUpdate: _instance.updateGlobalVolume} );
			}
			
			IS_MUTE = !IS_MUTE;
			
			_instance.dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function updateGlobalVolume ( ) : void
		{
			SoundMixer.soundTransform = new SoundTransform(temp_volume,0);
		}
		
		/**
		*
		*/
		public static function play ( snd : Sound, repeat : Boolean = false ) : SoundChannel
		{
			
			if ( _instance == null ) {
				_instance 			= new SoundManager();
				_soundChannelList 	= new Array();
			}
			
			try 
			{
				
				var channel : SoundChannel;
				var c : uint = 0;
				
				if ( repeat ) {
					c = 9999;
				}
				
				channel = snd.play( 0, c );
				channel.addEventListener( Event.SOUND_COMPLETE, _instance._onSoundComplete, false, 0, true);
				
				_soundChannelList.push( channel );
				
				return channel;
			}
			catch (err:Error)
			{
				throw("SoundManager error : "+ err.message);
			}
			
			return null;
		}
		
		public static function stop ( channel : SoundChannel ) : void
		{
			var i:uint;
			
			for (i = 0;i < _soundChannelList.length; i++)
			{
				if (_soundChannelList[i] == channel)
				{	
					channel.stop();
					_soundChannelList[i].removeEventListener(Event.SOUND_COMPLETE, _instance._onSoundComplete);
					_soundChannelList[i] = null;
					_soundChannelList.splice(i, 1);	
					break;
				}
			}
		}
		
		/**
		*
		*/
		protected function _onSoundComplete ( e : Event ) : void
		{
			var channel : SoundChannel = e.currentTarget as SoundChannel;
			
			channel.removeEventListener( Event.SOUND_COMPLETE, _onSoundComplete );
			removeSoundChannel( channel );
			
		}
		
		public static function addChannel ( channel:SoundChannel) : void
		{
			_soundChannelList.push(channel);
		}

		private static function removeSoundChannel(channel:SoundChannel):void
		{
			
			var i:uint;
			
			for (i = 0;i < _soundChannelList.length; i++)
			{
				if (_soundChannelList[i] == channel)
				{
					_soundChannelList[i].removeEventListener(Event.SOUND_COMPLETE, _instance._onSoundComplete);
					_soundChannelList[i] = null;
					_soundChannelList.splice(i, 1);	
					break;
				}
			}
		}
	}
}
